#!/bin/bash
echo "APF installation script"
echo -e "####################### \n"
if [ -f /etc/apf/conf.apf ]
then
echo "APF was already installed in this server"
exit
fi
if [ -d /root/installapf ]
then
rm -rf /root/installapf
fi
mkdir -p /root/installapf
cd /root/installapf
echo -e "Downloading APF .......... "
WGET_OUTPUT=$(2>&1 wget --timestamping --progress=dot:mega http://www.rfxnetworks.com/downloads/apf-current.tar.gz)
# Make sure the download went okay.
if [ $? -ne 0 ]
then
      # wget had problems.
      echo 1>&2 $0: "$WGET_OUTPUT"  Exiting.
      exit 1
fi


# Copy the file to the new name if necessary.
if echo "$WGET_OUTPUT" | fgrep 'saved' &> /dev/null
then
      echo "APF downloaded"
else
      echo "There is some problem with download"
exit;
fi
tar -xvzf apf-current.tar.gz > /dev/null
cd apf*
sh install.sh > /dev/null
echo "Installation Compleat"
echo "Backing up original configuration file..."
/bin/cp /etc/apf/conf.apf /etc/apf/conf.apf.old
echo "Moved to /etc/apf/conf.apf.old"
echo "Configuring APF"
echo "Setting DEVEL_MODE to zero"
#cd /etc/apf/
sed 's/DEVEL_MODE="1"/DEVEL_MODE="0"/g' /etc/apf/conf.apf > /etc/apf/conf.apf.tmp
`whereis mv | awk '{print $2}'` /etc/apf/conf.apf.tmp /etc/apf/conf.apf
echo "Changing network interface for VPS"
sed 's/IFACE_IN="eth0"/IFACE_IN="venet0"/g' /etc/apf/conf.apf > /etc/apf/conf.apf.tmp
`whereis mv | awk '{print $2}'` /etc/apf/conf.apf.tmp /etc/apf/conf.apf
sed 's/IFACE_OUT="eth0"/IFACE_OUT="venet0"/g' /etc/apf/conf.apf > /etc/apf/conf.apf.tmp
`whereis mv | awk '{print $2}'` /etc/apf/conf.apf.tmp /etc/apf/conf.apf
echo "Enabling MONOKERN"
sed 's/SET_MONOKERN="0"/SET_MONOKERN="1"/g' /etc/apf/conf.apf > /etc/apf/conf.apf.tmp
`whereis mv | awk '{print $2}'` /etc/apf/conf.apf.tmp /etc/apf/conf.apf
echo "Adding Ports to firewall......"
if [ -f /usr/local/cpanel/version ]
then
echo "Cpanel detected ...."
igtcp="21,25,26,53,80,110,143,443,2082,2083,2086,2087,2095,2096,3000_3500"
igudp="53"
elif [ -f /usr/local/psa/version ]
then
echo "Plesk detected ....."
igtcp="21,25,26,53,80,110,143,443,8443,3000_3500"
igudp="53"
elif [ -f /usr/local/directadmin/scripts/setup.txt ]
then
echo "DirectAdmin detected ....."
igtcp="21,25,26,53,80,110,143,443,587,2222,3000_3500"
igudp="53"
else
echo "Not found any control panels ... Using default configurations"
igtcp="21,25,26,53,80,110,143,443,10000,3000_3500"
igudp="53"
fi
shp=`sed -n '/^Port*/p' /etc/ssh/sshd_config | wc -l`
if [ $shp -eq 0 ]
then
sshport=22
else
sshport=`sed -n '/^Port*/p' /etc/ssh/sshd_config | awk '{print $2}'`
fi
igtcpssh=`echo $igtcp,$sshport`
ig_tcp=`echo "IG_TCP_CPORTS=\"$igtcpssh\""`
ig_udp=`echo "IG_UDP_CPORTS=\"$igudp\""`
sed s/'^IG_TCP_CPORTS="22"'/`echo $ig_tcp`/g /etc/apf/conf.apf > /etc/apf/conf.apf.tmp
`whereis mv | awk '{print $2}'` /etc/apf/conf.apf.tmp /etc/apf/conf.apf
sed s/'^IG_UDP_CPORTS=""'/`echo $ig_udp`/g /etc/apf/conf.apf > /etc/apf/conf.apf.tmp
`whereis mv | awk '{print $2}'` /etc/apf/conf.apf.tmp /etc/apf/conf.apf
echo "Following ports added : "
echo "$ig_tcp"
echo "$ig_udp"
echo -e "\nAPF configuration compleated................... \n"
echo "Check the configurations in /etc/apf/conf.apf and confirm that all the ports especially SSH,APACHE,MAIL ports are open"
echo "Start apf using apf -s"
echo -e "\nFor your information, listining ports in the server\n"
sh /etc/apf/extras/get_ports
exit
